package com.dalonedrau.jogl.opengl.ui.border;

import com.dalonedrau.jogl.engine.GameApplet;
import com.dalonedrau.jogl.engine.GlobalConsts;
import com.dalonedrau.jogl.engine.JOGLErrorHandler;
import com.dalonedrau.jogl.engine.Render;
import com.dalonedrau.jogl.opengl.sprite.BasicSpriteObject;
import com.dalonedrau.jogl.opengl.sprite.GroupOfImages;
import com.dalonedrau.jogl.opengl.sprite.SpriteImageObject;
import com.dalonedrau.jogl.opengl.sprite.SpriteImageObjectFactory;
import com.dalonedrau.vectors.Vector3;

/**
 * A simple border, similar to those found in the early 90's SNES games.
 * @author Donald
 */
public final class StatsBorder extends GuiBorder {
	/** the border group. */
	private GroupOfImages	borders;
	/**
	 * Creates a new instance of {@link StatsBorder}.
	 */
	public StatsBorder() {
		super.setInsets(
				GlobalConsts.INT_003,
				GlobalConsts.INT_003,
				GlobalConsts.INT_003,
				GlobalConsts.INT_003
				);
	}
	/** Positions all sprites for the border. */
	private void positionSprites() {
		if (super.getPosition() != null) {
			borders.setPosition(
					super.getPosition().getX(),
					super.getPosition().getY(),
					super.getPosition().getZ()
					);
			borders.clearFloatStore();
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void prepareForRendering() throws Exception {
		Render.getInstance().registerEntity(borders);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setPosition(final double x, final double y, final double z) {
		super.setPosition(x, y, z);
		positionSprites();
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setPreferredSize(final int width, final int height) {
		super.setPreferredSize(width, height);
		try {
			SpriteImageObject imgBL =
					SpriteImageObjectFactory.getInstance().getImageByName(
							"dw_stats_frame_bl");
			SpriteImageObject imgBR =
					SpriteImageObjectFactory.getInstance().getImageByName(
							"dw_stats_frame_br");
			SpriteImageObject imgB =
					SpriteImageObjectFactory.getInstance().getImageByName(
							"dw_stats_frame_b");
			SpriteImageObject imgTL =
					SpriteImageObjectFactory.getInstance().getImageByName(
							"dw_stats_frame_tl");
			SpriteImageObject imgTR =
					SpriteImageObjectFactory.getInstance().getImageByName(
							"dw_stats_frame_tr");
			SpriteImageObject imgT =
					SpriteImageObjectFactory.getInstance().getImageByName(
							"dw_stats_frame_t");
			SpriteImageObject imgL =
					SpriteImageObjectFactory.getInstance().getImageByName(
							"dw_stats_frame_l");
			SpriteImageObject imgR =
					SpriteImageObjectFactory.getInstance().getImageByName(
							"dw_stats_frame_r");
			if (borders == null) {
				borders = new GroupOfImages();

				{ // bottom left
					BasicSpriteObject sb = new BasicSpriteObject();
					sb.setImageRefId(imgBL.getRefId());
					sb.setWidth(imgBL.getWidth());
					sb.setHeight(imgBL.getHeight());
					borders.addImage(sb);
				}
				{ // bottom middle
					BasicSpriteObject sb = new BasicSpriteObject();
					sb.setImageRefId(imgB.getRefId());
					sb.setHeight(imgB.getHeight());
					borders.addImage(sb);
				}
				{ // bottom right
					BasicSpriteObject sb = new BasicSpriteObject();
					sb.setImageRefId(imgBR.getRefId());
					sb.setWidth(imgBR.getWidth());
					sb.setHeight(imgBR.getHeight());
					borders.addImage(sb);
				}
				{ // left
					BasicSpriteObject sb = new BasicSpriteObject();
					sb.setImageRefId(imgL.getRefId());
					sb.setWidth(imgL.getWidth());
					borders.addImage(sb);
				}
				{ // right
					BasicSpriteObject sb = new BasicSpriteObject();
					sb.setImageRefId(imgR.getRefId());
					sb.setWidth(imgR.getWidth());
					borders.addImage(sb);
				}
				{ // top left
					BasicSpriteObject sb = new BasicSpriteObject();
					sb.setImageRefId(imgTL.getRefId());
					sb.setWidth(imgTL.getWidth());
					sb.setHeight(imgTL.getHeight());
					borders.addImage(sb);
				}
				{ // top middle
					BasicSpriteObject sb = new BasicSpriteObject();
					sb.setImageRefId(imgT.getRefId());
					sb.setHeight(imgT.getHeight());
					borders.addImage(sb);
				}
				{ // top right
					BasicSpriteObject sb = new BasicSpriteObject();
					sb.setImageRefId(imgTR.getRefId());
					sb.setWidth(imgTR.getWidth());
					sb.setHeight(imgTR.getHeight());
					borders.addImage(sb);
				}
			}
			// set bottom-left position
			borders.getImage(0).setPosition(new Vector3(0, 0, 0));
			// set bottom position and width
			borders.getImage(1).setWidth(
					width - imgBL.getWidth() - imgBR.getWidth());
			borders.getImage(1).setPosition(
					new Vector3(imgBL.getWidth(), 0, 0));
			// set bottom-right position
			borders.getImage(2).setPosition(new Vector3(
					width - imgBR.getWidth(), 0, 0));
			// set left position and height
			borders.getImage(GlobalConsts.INT_003).setHeight(
					height - imgBL.getHeight() - imgTL.getHeight());
			borders.getImage(GlobalConsts.INT_003).setPosition(
					new Vector3(0, imgBL.getHeight(), 0));
			// set right position and height
			borders.getImage(GlobalConsts.INT_004).setHeight(
					height - imgBR.getHeight() - imgTR.getHeight());
			borders.getImage(GlobalConsts.INT_004).setPosition(
					new Vector3(width - imgR.getWidth(), imgBR.getHeight(), 0));
			// set top-left position
			borders.getImage(GlobalConsts.INT_005).setPosition(
					new Vector3(0, height - imgTL.getHeight(), 0));
			// set top position and width
			borders.getImage(GlobalConsts.INT_006).setWidth(
					width - imgTL.getWidth() - imgTR.getWidth());
			borders.getImage(GlobalConsts.INT_006)
					.setPosition(
							new Vector3(imgTL.getWidth(), height
									- imgT.getHeight(), 0));
			// set top-right position
			borders.getImage(GlobalConsts.INT_007).setPosition(
					new Vector3(width - imgTR.getWidth(),
							height - imgTR.getHeight(), 0));

			positionSprites();
			imgTL = null;
			imgT = null;
			imgTR = null;
			imgL = null;
			imgR = null;
			imgBL = null;
			imgB = null;
			imgBR = null;
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
}
